//商家信息列表
var sellers = new Vue({
  el: "#sellers",
  data: {
    sellers: [],
    isModifying: [],
    currentSearchPattern: "",
    matchSellers: [],
    isSearching: false,
    isAdding: false
  },
  methods: {
    addItem: function () {
      this.isAdding = true;
      console.log("add item");
      this.$http.get(SERVER_PORT + "requestAllStore").then(response => {
        this.sellers = Object.assign([], response.data.data);
        for (let i = 0; i < this.sellers.length; i++) {
          this.isModifying.push(false);
        }
        this.sellers.unshift({});
        this.isModifying.unshift(true);
      });
    },
    modifyItem: function (index) {
      console.log("change item " + index + " to modifying state");
      this.$set(this.isModifying, index, true);
    },
    setSellerInfo: function (index, callback) {
      if (index === 0 && this.isAdding === true) {
        Vue.http
          .post(
            SERVER_PORT + "addStore",
            {
              store_name: sellers.sellers[index].store_name,
              phone: sellers.sellers[index].phone,
              latitude: sellers.sellers[index].latitude,
              longitude: sellers.sellers[index].longitude,
              address: sellers.sellers[index].address
            },
            { emulateJSON: true }
          )
          .then(response => {
            callback(response);
          });
      }
      else {
        Vue.http
          .post(
            SERVER_PORT + "modifyStore",
            {
              store_ID: sellers.sellers[index].store_ID,
              store_name: sellers.sellers[index].store_name,
              phone: sellers.sellers[index].phone,
              latitude: sellers.sellers[index].latitude,
              longitude: sellers.sellers[index].longitude,
              address: sellers.sellers[index].address
            },
            { emulateJSON: true }
          )
          .then(response => {
            callback(response);
          });
      }
    },
    submitItem: function (index) {
      Ply.dialog("confirm", "确定提交?").done(function () {
        console.log("submit item " + index);
        sellers.setSellerInfo(index, function (response) {
          if (response.data.status === 1) {
            Ply.dialog("alert", "提交成功！");
            Vue.set(sellers.isModifying, index, false);
          } else {
            Ply.dialog("alert", "提交失败，请检查！");
          }
        });
        console.log(sellers.sellers);
        Vue.set(sellers.isModifying, index, false);
        if (index === 0 && sellers.isAdding === true) sellers.isAdding = false;
      });
    },
    deleteItem: function (index) {
      Ply.dialog("confirm", "确定删除?").done(function () {
        console.log("delete item " + index);
        // Vue.http
        //   .post(
        //     SERVER_PORT + "deleteStore",
        //     {
        //       store_ID: sellers.sellers[index].store_ID
        //     },
        //     { emulateJSON: true }
        //   )
        //   .then(response => {
        //     if (response.data.status === 1) {
        //       Ply.dialog("alert", "删除成功！");
        //       sellers.sellers.splice(index, 1);
        //       sellers.isModifying.splice(index, 1);
        //     } else {
        //       Ply.dialog("alert", "删除失败，请重试！");
        //     }
        //   });
        sellers.sellers.splice(index, 1);
        sellers.isModifying.splice(index, 1);
      });
    },
    requsetAllDataFromServer: function () {
      this.$http.get(SERVER_PORT + "requestAllStore").then(response => {
        console.log(response.data.data);
        //this.$set(this.sellers,response);\
        this.sellers = Object.assign([], response.data.data);
        for (let i = 0; i < this.sellers.length; i++) {
          this.isModifying.push(false);
        }
      });
    },
    performSearch: function () {
      this.isSearching = true;
      this.$http.get(SERVER_PORT + "requestAllStore").then(
        response => {
          this.sellers = Object.assign([], response.data.data);
          for (let i = 0; i < this.sellers.length; i++) {
            this.isModifying.push(false);
          }
          if (this.currentSearchPattern !== "") {
            this.matchSellers = [];
            for (let seller of this.sellers) {
              for (let attr in seller) {
                if (attr === "store_ID") continue;
                if (seller[attr].toString().match(this.currentSearchPattern)) {
                  this.matchSellers.push(seller);
                  break;
                }
              }
            }
            this.sellers = Object.assign([], this.matchSellers);
            this.isModifying = Object.assign([], []);
            for (let i = 0; i < this.matchSellers.length; i++) {
              this.isModifying.push(false);
            }
          }
          this.isSearching = false;
        },
        response => {
          // 响应错误回调
        }
      );
    }
  },
  created: function () {
    console.log("enter");
    this.requsetAllDataFromServer();
  }
});
